
package com.afd.ozblip.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.afd.ozblip.adapter.ActivityListAdapter;
import com.afd.ozblip.entity.ActivityEntity;
import com.afd.ozblip.net.HttpUtil;
import com.afd.ozblip.util.AsyncAction;
import com.afd.ozblip.util.AsyncLoadData;
import com.afd.ozblip.util.CollectionUtil;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.ParseJson;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ActivityListActivity extends BaseActivity {

    /****************** 视图变量 **************************/
    private LinearLayout toAddActivityLayout;
    private ListView nowActivityListView;
    private ListView oldActivityListView;
    private TextView nowListTipTv;
    private TextView oldListTipTv;
    private LinearLayout noDataLayout;
    private RelativeLayout hasDataLayout;
    /****************** 其它变量 **************************/

    // AIzaSyBXBjw7UxLsOhNsTtAHy5cCS1yb-ObaseY
    private List<ActivityEntity> nowActivityList;
    private List<ActivityEntity> oldActivityList;
    private ActivityListAdapter nowActivityListAdapter;
    private ActivityListAdapter oldActivityListAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        // 启动返回功能
        toBack();
        // 寻找视图
        findViews();
        // 设置监听
        setListeners();
        // 获取参数
        getBundle();
        // 获取当前活动列表数据
        requestNowActivityListData();
        // 获取往期活动列表数据
        requestActivityListData();
    }

    /**
     * 寻找视图
     */
    public void findViews() {
        this.toAddActivityLayout = (LinearLayout) findViewById(R.id.ll_activity_toAdd);
        this.nowActivityListView = (ListView) findViewById(R.id.lv_activity_list1);
        this.oldActivityListView = (ListView) findViewById(R.id.lv_activity_list2);
        this.noDataLayout = (LinearLayout) findViewById(R.id.ll_activity_tip);
        this.hasDataLayout = (RelativeLayout) findViewById(R.id.rl_activity_list);
        this.nowListTipTv = (TextView) findViewById(R.id.tv_list_data_tip);
        this.oldListTipTv = (TextView) findViewById(R.id.tv_list_data_tip1);
        // View oldHeaderV = LayoutInflater.from(this).inflate(
        // R.layout.activity_old_header, null);
        // this.oldActivityListView.addHeaderView(oldHeaderV);
    }

    /**
     * 设置监听
     */
    public void setListeners() {
        View.OnClickListener activityClick = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.ll_activity_toAdd:
                        Intent settingIntent = new Intent(
                                ActivityListActivity.this,
                                AddActivityActivity.class);
                        startActivity(settingIntent);
                        break;

                    default:
                        break;
                }
            }
        };
        this.toAddActivityLayout.setOnClickListener(activityClick);
        this.nowActivityListView
                .setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> itemView, View arg1,
                            int parameInt, long arg3) {
                        ActivityEntity activity = ActivityListActivity.this.nowActivityList
                                .get(parameInt);
                        Intent detailIntent = new Intent(
                                ActivityListActivity.this,
                                ActivityDetailActivity.class);
                        detailIntent.putExtra("activityInfo", activity);
                        startActivity(detailIntent);
                    }
                });
        this.oldActivityListView
                .setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> itemView, View arg1,
                            int parameInt, long arg3) {
                        ActivityEntity activity = ActivityListActivity.this.oldActivityList
                                .get(parameInt);
                        Intent detailIntent = new Intent(
                                ActivityListActivity.this,
                                ActivityDetailActivity.class);
                        detailIntent.putExtra("activityInfo", activity);
                        startActivity(detailIntent);
                    }
                });

    }

    public void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        // ((MarginLayoutParams) params).setMargins(10, 10, 10, 10);
        listView.setLayoutParams(params);
    }

    /**
     * 获取传递的参数
     */
    private void getBundle() {
        // Intent intent = getIntent();
    }

    /**
     * 获取进行中的活动列表数据
     */
    private void requestNowActivityListData() {
        this.nowListTipTv.setVisibility(0);
        this.nowListTipTv.setText(getResources()
                .getString(R.string.loding_text));
        AsyncLoadData ald = new AsyncLoadData(this, "", new AsyncAction() {

            @Override
            public void doLoad() throws Exception {
                // 获取提交参数
                Map<String, Object> activityParamMap = new HashMap<String, Object>();
                activityParamMap.put("pageCnt", "1");
                activityParamMap.put("pageRow", "30");
                activityParamMap.put("type", 1);
                activityParamMap.put("language", commonLanguage);
                // 发送请求
                byte[] commentItemList = HttpUtil.requestByPost(
                        ActivityListActivity.this,
                        Constants.SELLER_ACTIVITY_LIST_API, activityParamMap);
                if (commentItemList != null) {
                    nowActivityList = new ParseJson()
                            .parseBusinessActivityListJson(commentItemList);
                }
            }

            @Override
            public void loadEnd() throws Exception {
                setNowActivityListAdapter();
                setListViewHeightBasedOnChildren(nowActivityListView);
            }

        });
        ald.execute(0);
    }

    /**
     * 获取往前活动列表数据
     */
    private void requestActivityListData() {
        this.oldListTipTv.setVisibility(0);
        this.oldListTipTv.setText(getResources()
                .getString(R.string.loding_text));
        AsyncLoadData ald = new AsyncLoadData(this, "", new AsyncAction() {

            @Override
            public void doLoad() throws Exception {
                // 获取提交参数
                Map<String, Object> activityParamMap = new HashMap<String, Object>();
                activityParamMap.put("pageCnt", "1");
                activityParamMap.put("pageRow", "20");
                activityParamMap.put("type", 2);
                activityParamMap.put("language", commonLanguage);
                // 发送请求
                byte[] commentItemList = HttpUtil.requestByPost(
                        ActivityListActivity.this,
                        Constants.SELLER_ACTIVITY_LIST_API, activityParamMap);
                if (commentItemList != null) {
                    oldActivityList = new ParseJson()
                            .parseBusinessActivityListJson(commentItemList);
                }
            }

            @Override
            public void loadEnd() throws Exception {
                setOldActivityListAdapter();
                setListViewHeightBasedOnChildren(oldActivityListView);
            }

        });
        ald.execute(0);
    }

    /**
     * 调用活动列表适配器
     */
    public void setNowActivityListAdapter() {
        if (CollectionUtil.UnNullAndZero(nowActivityList)) {
            this.nowListTipTv.setVisibility(8);
            this.nowActivityListAdapter = new ActivityListAdapter(
                    ActivityListActivity.this);
            this.nowActivityListAdapter.addItems(nowActivityList);
            this.nowActivityListView.setAdapter(nowActivityListAdapter);
        } else {
            this.nowListTipTv.setVisibility(8);
            this.hasDataLayout.setVisibility(8);
            this.noDataLayout.setVisibility(0);
        }
    }

    /**
     * 调用往期列表适配器
     */
    public void setOldActivityListAdapter() {
        if (CollectionUtil.UnNullAndZero(oldActivityList)) {
            this.oldListTipTv.setVisibility(8);
            this.oldActivityListAdapter = new ActivityListAdapter(
                    ActivityListActivity.this);
            this.oldActivityListAdapter.addItems(oldActivityList);
            this.oldActivityListView.setAdapter(oldActivityListAdapter);
        } else {
            this.oldListTipTv.setVisibility(0);
            this.oldListTipTv.setText(getResources().getString(
                    R.string.noData_text));
        }
    }
}
